当前位置:  开发笔记 > 编程语言 > 正文

Django`manage.py test`忽略了一些测试

如何解决《Django`manage.pytest`忽略了一些测试》经验,为你挑选了1个好方法。

我有一些基于django的应用程序.在这个应用程序内我有tests.py文件.在这个文件里面我有两个测试类:

# -*- coding: utf-8 -*-

from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from translations import utils, views_ajax
from translations.models import Project
import json


def print_results(sentences):
    for sent in sentences:
        print "u\"" + sent + "\","


class TextSplitTest(TestCase):
    maxDiff = None

    def test_en_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'en')
        self.assertEqual(sentences, good_result)

    def test_fr_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'ru')
        self.assertEqual(sentences, good_result)

    def test_zh_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'zh')
        self.assertEqual(sentences, good_result)

    def test_es_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'es')
        # print_results(sentences)
        self.assertEqual(sentences, good_result)


class CreateProjectTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(username='jacob', email='jacob@gmail.com', password='top_secret')

    def create_project(self):
        request = self.factory.post('/api/project-create/',
                                    data=json.dumps({'name': 4321, 'description': "ololo", 'type': "public"}),
                                    content_type='application/json')
        request.user = self.user
        response = views_ajax.create_project_ajax(request)

        all_user_projects = Project.objects.filter(manager=self.user)

        self.assertEqual(len(all_user_projects), 1)
        self.assertEqual(response.status_code, 200)

    def add_text_to_project(self):
        project = Project.objects.get(manager=self.user, name='4321')
        data = json.dumps({"project":project.id,
                           "title":"French test",
                           "subject":1,
                           "sourceLang":7,
                           "targetLang":2,
                           "textBody":"Some text"})

        request = self.factory.post('/api/text/', data=data, content_type='application/json')
        request.user = self.user

        response = views_ajax.text_ajax(request)

        self.assertEqual(response.status_code, 200)

但是当我试图开始测试时,我得到了:

# python manage.py test -v 2
...
test_basic_addition (entries.tests.SimpleTest) ... ok
test_en_split (translations.tests.TextSplitTest) ... ok
test_es_split (translations.tests.TextSplitTest) ... ok
test_fr_split (translations.tests.TextSplitTest) ... ok
test_ru_split (translations.tests.TextSplitTest) ... ok
test_zh_split (translations.tests.TextSplitTest) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.224s

OK

当我尝试启动精确的测试类时:

# python manage.py test translations.tests.CreateProjectTest -v 2
...
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

为什么django忽略了一些测试?我试图谷歌它,但什么都没得到:(



1> Arjen Dijkst..:

您要运行的测试应该以test测试开头:

def test_create_project(self)

等等...

https://docs.python.org/2/library/unittest.html#basic-example

使用名称以字母test开头的方法定义三个单独的测试.此命名约定通知测试运行器哪些方法代表测试.

Django docs:https: //docs.djangoproject.com/en/1.9/topics/testing/overview/#writing-tests

运行测试时,测试实用程序的默认行为是在名称以test开头的任何文件中查找所有测试用例(即unittest.TestCase的子类),从这些测试用例中自动构建测试套件,并运行该套件.

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有